Python Basics - Lesson 02 - Making Decisions with if

Python Basics · Lesson 02

Making Decisions with if

Programs can respond differently depending on the information they receive. In Python, if, elif, and else let you choose which instructions to run.

By the end, you can: compare values, write an if/else branch, choose between several cases with elif, and combine conditions with and or or.

1. Comparisons produce True or False

A condition is an expression Python can evaluate as True or False. These comparison operators are common:

  • == equal to
  • != not equal to
  • < less than and > greater than
  • <= less than or equal to and >= greater than or equal to
temperature = 18
print(temperature < 20)

This displays True because 18 is less than 20.

2. Run code only when a condition is true

Put a colon after the condition. Indent the instructions that belong to that branch.

temperature = 18

if temperature < 20:
    print("Bring a light jacket.")

If the condition is false, Python skips the indented block. Add else to provide another path:

temperature = 24

if temperature < 20:
    print("Bring a light jacket.")
else:
    print("A light layer should be enough.")

3. Choose from several cases with elif

Python checks conditions from top to bottom and runs the first matching branch. Use elif for additional cases.

score = 82

if score >= 90:
    print("Excellent work!")
elif score >= 70:
    print("You are on track.")
elif score >= 50:
    print("Review a few topics.")
else:
    print("Start with another practice round.")

The order matters: the highest score range is checked first. Once a condition is true, Python skips the remaining branches.

4. Combine conditions

Use and when both conditions must be true. Use or when either one can be true.

has_ticket = True
age = 15

if has_ticket and age >= 13:
    print("You can enter the event.")

Hands-on exercises

  1. Ask the user for a whole number. Print whether it is positive, negative, or zero.
  2. Ask for a test score from 0 to 100. Print “Excellent” for 90 or higher, “Passing” for 60–89, and “Keep practicing” for anything below 60.
Show Solution & Common Mistakes

Exercise 1 — one possible solution

number = int(input("Enter a whole number: "))

if number > 0:
    print("Positive")
elif number < 0:
    print("Negative")
else:
    print("Zero")

Exercise 2 — one possible solution

score = int(input("Enter a score from 0 to 100: "))

if score >= 90:
    print("Excellent")
elif score >= 60:
    print("Passing")
else:
    print("Keep practicing")

Common mistakes:

  • Using = to compare. Use == for equality; = assigns a value.
  • Forgetting the colon at the end of if, elif, or else.
  • Indenting the branch inconsistently. The statements inside a branch must share the same indentation.
  • Putting a broad condition before a narrower one. Python runs only the first matching branch.

Quick quiz

Choose an answer, then select Check answer.

1. Which operator checks whether two values are equal?

2. What does the score example print when score = 82?

3. When does an else branch run?

Mini-project: Weather Outfit Advisor

Write a small program that recommends what to wear based on the temperature.

Requirements:

  • Ask the user for the temperature in Celsius.
  • Below 10°C, recommend a warm coat.
  • From 10°C up to (but not including) 20°C, recommend a light jacket.
  • At 20°C or higher, recommend comfortable light clothing.
  • Print the recommendation in a complete sentence.

Hint: Convert the input with int(). Check the coldest range first, then use elif for the next range and else for the remaining temperatures.

Example run:

Temperature in Celsius: 14
For 14°C, a light jacket is a good choice.
Show Full Project Solution
temperature = int(input("Temperature in Celsius: "))

if temperature < 10:
    outfit = "a warm coat"
elif temperature < 20:
    outfit = "a light jacket"
else:
    outfit = "comfortable light clothing"

print(f"For {temperature}°C, {outfit} is a good choice.")

Notice that the second condition only checks whether the temperature is below 20. Temperatures below 10 were already handled by the first branch, so the remaining values are from 10°C up to 19°C.

Key takeaways

  • Comparisons evaluate to True or False.
  • if and else create two paths through a program.
  • elif adds more choices; the first true branch wins.
  • Use and when both conditions must be true.

Posts les plus consultés de ce blog

Python Programming Exercises with Solutions - Complete Beginner's Guide

Python Basics - Capstone - Study Sprint Tracker

Python Intermediate - Lesson 10 - Working with Excel Spreadsheets